package com.s3.khospitalds.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.s3.khospitalds.common.ResponseEntity;
import com.s3.khospitalds.entity.Doctor;
import com.s3.khospitalds.entity.DoctorResignInfo;
import com.s3.khospitalds.entity.Hasnurse;
import com.s3.khospitalds.mapper.HasnurseMapper;
import com.s3.khospitalds.service.HasnurseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * @Author: xw
 * @CreateTime: 2025/2/20
 * @Description: 护士
 * @Version: 1.0
 */
@Service
public class HasnurseServiceImpl extends ServiceImpl<HasnurseMapper,Hasnurse> implements HasnurseService {
    @Autowired
    private HasnurseServiceImpl hasnurseService;
    @Autowired
    private HasnurseMapper hasnurseMapper;

    @Override
    public List<Hasnurse> getAllHasnurse() {
        return hasnurseMapper.getAllHasnurse();
    }

    @Override
    public int insertHasnurse(Hasnurse hasnurse) {
        return hasnurseMapper.insertHasnurse(hasnurse);
    }

    @Override
    public int updateHasnurse(Hasnurse hasnurse) {
        // 获取 doctor 的 date 字段
        Date date = hasnurse.getDate();

        // 将 Date 转换为 LocalDate
        LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

        // 增加一天
        LocalDate newLocalDate = localDate.plusDays(1);

        // 将 LocalDate 转换回 Date
        Date newDate = Date.from(newLocalDate.atStartOfDay(ZoneId.systemDefault()).toInstant());

        // 更新 doctor 对象的 date 字段
        hasnurse.setDate(newDate);

        return hasnurseMapper.updateHasnurse(hasnurse);
    }

    @Override
    public int deleteHasnurse(DoctorResignInfo doctorResignInfo) {
        //return hasnurseMapper.deleteHasnurse(id);
        return hasnurseMapper.updateHasnurseResign(doctorResignInfo);
    }

    @Override
    public List<Hasnurse> findByDepartmentAndName(String department, String name) {
        return hasnurseMapper.findByDepartmentAndName(department, name);
    }

    @Override
    public ResponseEntity getHasnurseService(int pageNum, int pageSize, String search) {
        ResponseEntity responseEntity = new ResponseEntity();
        //开启分页
        PageHelper.startPage(pageNum,pageSize);
        //模糊查询
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",search);
        queryWrapper.eq("isexpert",1);
        //使用分页查询
        List<Hasnurse> hasnurse = hasnurseService.list(queryWrapper);
        //创建分页的对象
        PageInfo<Hasnurse> pageInfo = new PageInfo<>(hasnurse);
        //设置响应对象的值
        responseEntity.setCount((int) pageInfo.getTotal());
        //设置响应对象的行数据
        responseEntity.setData(pageInfo.getList());
        return responseEntity;
    }

    @Override
    public ResponseEntity getResignHasnurse() {
        ResponseEntity responseEntity = new ResponseEntity();
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("isexpert",2);
        List<Doctor> hasnurse = hasnurseService.list(queryWrapper);
        responseEntity.setData(hasnurse);
        return responseEntity;
    }
}
